package com.cssy.xxkj.basic.web;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.basic.entity.BasicCurrencyTestDO;
import com.cssy.xxkj.basic.entity.BasicSeriesDO;
import com.cssy.xxkj.basic.service.IBasicCurrencyTestService;
import com.cssy.xxkj.basic.service.IBasicSeriesService;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;
/**
 * 货币管理
 * @author hf
 *
 */
@Controller
@RequestMapping("/basic/series")
public class SeriesController extends BaseController<BasicSeriesDO> {
	private static final String MANAGER = "/xxkj/basic/series/SeriesManager";// 列表页面
	private static final String EIDT = "/xxkj/basic/series/SeriesOperation";// 添加、修改、显示页面
	private static final String BOMEIDT = "/xxkj/basic/series/SeriesBomOperation";// 默认添加、修改、显示页面

	@Resource(name = "basicSeriesService")
	private IBasicSeriesService basicSeriesService;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;

	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		return MANAGER;
	}
	
	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		 JqGridHandler hander=new JqGridHandler(request);
		 SearchConditions scs = new SearchConditions();
		 hander.getWheres(scs);
		 scs.setRemovedProperty("removed");
		 SearchResult result= basicSeriesService.findListByExample(scs);
		 OutJsonGrid out=new OutJsonGrid(result);
		 out.printJSon(response);

	}

	@RequestMapping(value = "/operationBom/{flag}/{id}")
	public String operationBom(HttpServletRequest request,@PathVariable String id,@PathVariable String flag, Model model) throws Exception{
		BasicSeriesDO bcd =basicSeriesService.findListById(id);
		model.addAttribute("series", bcd);
		return BOMEIDT;
	}
	
	
	@Override
	public String operationForm(HttpServletRequest request,
			@PathVariable String id, @PathVariable String flag, Model model)
			throws Exception {
		
		if (OperationFlag.ADD.equals(flag)) {// 我新增加时操作
			model.addAttribute("flag", "ADD");
		} else {
			BasicSeriesDO bcd =basicSeriesService.findListById(id);
			model.addAttribute("series", bcd);
			model.addAttribute("flag", "EDIT");
		}
		return EIDT;
	}
	
	@Override
	public Map<String, Object> save(HttpServletRequest request,
			BasicSeriesDO curAP, BindingResult result, Model model) {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			basicSeriesService.SbomSave(request, curAP);
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		return bodyMap.modelMap;

	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String isDelete = request.getParameter("isDelete");//标识是否是假删除
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			for(String field : fields){
				if(isDelete==null||"0".equals(isDelete)){
					basicSeriesService.delete(field, SessionUtil.getUser(request));
				}else{
					basicSeriesService.deleteBizRemoved(field);
				}
			}
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed() + e.getMessage());
		}
		return bodyMap.modelMap;
	}

}
